package com.platform.service.impl;

import com.platform.dao.AmountBillDao;
import com.platform.dao.ApiResourceDao;
import com.platform.dao.UserDao;
import com.platform.entity.AmountBillEntity;
import com.platform.entity.ApiResourceEntity;
import com.platform.entity.UserEntity;
import com.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-16 15:02:28
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private AmountBillDao amountBillDao;
    @Autowired
    private ApiResourceDao apiResourceDao;

    @Override
    public UserEntity queryObject(Long id) {
        return userDao.queryObject(id);
    }

    @Override
    public List<UserEntity> queryList(Map<String, Object> map) {
        return userDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return userDao.queryTotal(map);
    }

    @Override
    public int save(UserEntity apiUser) {
        userDao.save(apiUser);
        List<ApiResourceEntity> ispics = apiUser.getPics();
        for (ApiResourceEntity info : ispics) {
            info.setIsShow(0);
            info.setMouldId(apiUser.getId());
            info.setMouldName("userVo");
            info.setType(1);
            apiResourceDao.update(info);
        }

        return 1;
    }

    @Override
    public int update(UserEntity apiUser) {
        /**
         * 先隐藏之前的数据
         */
        Map<String, Object> params = new HashMap<>();
        params.put("mouldId", apiUser.getId());
        params.put("type", 1);
        params.put("isShow", 0);
        List<ApiResourceEntity> pics = apiResourceDao.queryList(params);
        for (ApiResourceEntity info : pics) {
            info.setIsShow(1);
            apiResourceDao.update(info);
        }
        List<ApiResourceEntity> ispics = apiUser.getPics();
        for (ApiResourceEntity info : ispics) {
            info.setIsShow(0);
            info.setMouldId(apiUser.getId());
            info.setMouldName("userVo");
            info.setType(1);
            apiResourceDao.update(info);
        }
        return userDao.update(apiUser);
    }

    @Override
    public int delete(Long id) {
        return userDao.delete(id);
    }

    @Override
    public int deleteBatch(Long[] ids) {
        return userDao.deleteBatch(ids);
    }

    @Override
    public UserEntity selectByMobile(String invitationmobile) {
        return userDao.selectByMobile(invitationmobile);
    }

    @Override
    public Boolean reback(String add, Long userId, BigDecimal amount, Long mouldId) {

        UserEntity userEntity = userDao.queryObject(userId);
        if (userEntity != null) {
            userEntity.setAvailableAmount(userEntity.getAvailableAmount().add(amount));
            userEntity.setPresentAmount(userEntity.getPresentAmount().add(amount));
            userDao.update(userEntity);
            //添加流水
            AmountBillEntity amountBill = new AmountBillEntity();
            amountBill.setUserId(userEntity.getId());
            amountBill.setCreateTime(new Date());
            amountBill.setAmount(amount);
            amountBill.setStatus(3);
            amountBill.setTitle("提现失败");
            amountBill.setType(2);
            amountBill.setMouldId(mouldId);
            amountBillDao.save(amountBill);
            return true;
        }
        return false;
    }
}
